/*
 * Copyright (C) 2013 AndXmppSDK Project <hljdrl@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.drl.xmpp;

import org.drl.xmpp.util.StringHelper;
import org.jivesoftware.smack.packet.DefaultPacketExtension;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.packet.Presence;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * 
 * 用户、好友 登录状态对象类,离线、在线、离开、我有空、忙碌等状态.
 * @author hljdrl@gmail.com
 * 
 */
public final class TPresence implements Parcelable {
	public static final Parcelable.Creator<TPresence> CREATOR = new Parcelable.Creator<TPresence>() {

		@Override
		public TPresence createFromParcel(Parcel source) {
			return new TPresence(source);
		}

		@Override
		public TPresence[] newArray(int size) {
			return new TPresence[size];
		}
	};

	private int 	 mType;
	private int 	 mStatus;
	private String   mTo;
	private String   mFrom;
	private String   mStatusText;
	private String   mAvatorHash;
	private boolean  hashExists;
	
	/**
	 * @param source 序列化
	 */
	public TPresence(Parcel source) {
		mType = source.readInt();
		mStatus = source.readInt();
		mTo = source.readString();
		mFrom = source.readString();
		mStatusText = source.readString();
		hashExists = Boolean.parseBoolean(source.readString());
		mAvatorHash = source.readString();
	}
	/**
	 * @return 返回类型 ,Presence.getType()
	 */
	public final int getType()
	{
		return mType;
	}
	/**
	 * @return 返回用户状态,对应 Presence.getMode()方法
	 */
	public final int getStatus()
	{
		return mStatus;
	}
	/**
	 * @return 返回用户的登录状态-文件描述
	 */
	public final String getStatusText(){
		return mStatusText;
	}
	/**
	 * @param presence 用户状态
	 */
	public TPresence(final Presence presence) {
		mType = TPresence.getPresenceType(presence);
		mStatus = PresenceStatus.getStatusFromPresence(presence);
		mTo = presence.getTo();
		mFrom = presence.getFrom();
		mStatusText = presence.getStatus();
		final PacketExtension packetExtension = presence.getExtension("x",
				"vcard-temp:x:update");
		if (packetExtension != null
				&& packetExtension instanceof DefaultPacketExtension) {
			DefaultPacketExtension o = (DefaultPacketExtension) packetExtension;
			String _hash = o.getValue("photo");
			if (_hash != null) {
				this.mAvatorHash = _hash;
			}
		}
		
	}
	/**
	 * 是否有头像
	 * @return 是否有头像
	 */
	public final boolean hasAvator()
	{
		if(StringHelper.isText(mAvatorHash)){
			return true;
		}
		return false;
	}

	@Override
	public int describeContents() {
		return this.hashCode();
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(mType);
		dest.writeInt(mStatus);
		dest.writeString(mTo);
		dest.writeString(mFrom);
		dest.writeString(mStatusText);
		dest.writeString(Boolean.toString(hashExists));
		dest.writeString(mAvatorHash);
	}

	// ---------------------------------------------------------------
	/** The user is available to receive messages (default). */
	public static final int AVAILABLE = 100;

	/** The user is unavailable to receive messages. */
	public static final int UNAVAILABLE = 200;

	/** Request subscription to recipient's presence. */

	public static final int SUBSCRIBE = 300;

	/** Grant subscription to sender's presence. */
	public static final int SUBSCRIBED = 400;

	/** Request removal of subscription to sender's presence. */
	public static final int UNSUBSCRIBE = 500;

	/** Grant removal of subscription to sender's presence. */
	public static final int UNSUBSCRIBED = 600;

	/** The presence packet contains an error message. */
	public static final int ERROR = 701;

	/**
	 * Get the presence type from a presence packet.
	 * 
	 * @param presence
	 *            the presence type
	 * @return an int representing the presence type
	 */
	public final static int getPresenceType(final Presence presence) {
		int res = ERROR;
		switch (presence.getType()) {
		case available:
			res = TPresence.AVAILABLE;
			break;
		case unavailable:
			res = TPresence.UNAVAILABLE;
			break;
		case subscribe:
			res = TPresence.SUBSCRIBE;
			break;
		case subscribed:
			res = TPresence.SUBSCRIBED;
			break;
		case unsubscribe:
			res = TPresence.UNSUBSCRIBE;
			break;
		case unsubscribed:
			res = TPresence.UNSUBSCRIBED;
			break;
		case error:
		default:
			res = TPresence.ERROR;
		}
		return res;
	}

	/**
	 * Get the smack presence mode for a status.
	 * 
	 * @param type
	 *            the status type in beem
	 * @return the presence mode to use in presence packet or null if there is
	 *         no mode to use
	 */
	public final static Presence.Type getPresenceTypeFrom(final int type) {
		Presence.Type res;
		switch (type) {
		case AVAILABLE:
			res = Presence.Type.available;
			break;
		case UNAVAILABLE:
			res = Presence.Type.unavailable;
			break;
		case SUBSCRIBE:
			res = Presence.Type.subscribe;
			break;
		case SUBSCRIBED:
			res = Presence.Type.subscribed;
			break;
		case UNSUBSCRIBE:
			res = Presence.Type.unsubscribe;
			break;
		case UNSUBSCRIBED:
			res = Presence.Type.unsubscribed;
			break;
		case ERROR:
			res = Presence.Type.error;
			break;
		default:
			return null;
		}
		return res;
	}

}
